/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
import DTO.WebsiteDTO;
import DAO.WebsiteDAO;
import javax.swing.JOptionPane;

/**
 *
 * @author QuangHau
 */
public class Website extends javax.swing.JDialog {

    /**
     * Creates new form Website
     */
    
    public boolean isReset = false;
    private int maWebsite;
    private int status;
    
    public Website(java.awt.Frame parent, boolean modal, int _status, int _maWebsite) {
        super(parent, modal);
        initComponents();
        status = _status;
        maWebsite = _maWebsite;
        if(status == 1){
            btnThem.setText("Cập nhật");
            this.setTitle("Cập nhật website");
            WebsiteDTO websiteDTO =  WebsiteDAO.ThongTinWebsite(maWebsite);
            txtWebsite.setText(websiteDTO.getTenWebsite());
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtWebsite = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        btnHuy = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm Website");

        jLabel1.setText("Tên website:");

        txtWebsite.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtWebsiteCaretUpdate(evt);
            }
        });

        btnThem.setText("Thêm");
        btnThem.setEnabled(false);
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnHuy.setText("Hủy");
        btnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHuyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtWebsite)
                .addGap(37, 37, 37))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(101, Short.MAX_VALUE)
                .addComponent(btnThem)
                .addGap(53, 53, 53)
                .addComponent(btnHuy)
                .addContainerGap(100, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnHuy, btnThem});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtWebsite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnHuy))
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        if (txtWebsite.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Tên website không được để trống", "Lỗi", JOptionPane.ERROR_MESSAGE);
        } else {
            WebsiteDTO web = new WebsiteDTO();
            web.setTenWebsite(txtWebsite.getText().trim());
            if (status == 0) {
                //them website 
                if (WebsiteDAO.insertWebsite(web)) {
                    JOptionPane.showMessageDialog(null, "Thêm Thành Công", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                    isReset = true;
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Thêm thất bại", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                }
            } else {
                web.setMaWebsite(maWebsite);
                if (WebsiteDAO.updateWebsite(web)) {
                    JOptionPane.showMessageDialog(null, "Cập nhật thành Công", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                    isReset = true;
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Cập nhật thất bại", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHuyActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnHuyActionPerformed

    private void txtWebsiteCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtWebsiteCaretUpdate
        if (txtWebsite.getText().trim().equals("")) {
            btnThem.setEnabled(false);
        } else {
            btnThem.setEnabled(true);
        }
    }//GEN-LAST:event_txtWebsiteCaretUpdate

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Website.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Website.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Website.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Website.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Website dialog = new Website(new javax.swing.JFrame(), true, 0, -1);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHuy;
    private javax.swing.JButton btnThem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtWebsite;
    // End of variables declaration//GEN-END:variables

   
}
